package org.springblade.develop.support;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.medusa.gruul.common.core.util.StringUtil;
import lombok.AllArgsConstructor;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 代码模版生成实现类
 * 继承BeetlTemplateEngine
 * 重写entity controller mapper service的输出方法.
 *
 * @author Chill
 */
@AllArgsConstructor
public class ByTemplateEngine extends BeetlTemplateEngine {

	private String apiOutputDir;

	private String serverOutputDir;

	@Override
	protected void outputCustomFile(List<CustomFile> customFiles, TableInfo tableInfo, Map<String, Object> objectMap) {
		String packageName = String.valueOf(objectMap.get("packageName"));
		String entityName = String.valueOf(objectMap.get("entityName"));
		customFiles.forEach(customFile -> {
			String key = customFile.getFileName();
			String value = customFile.getTemplatePath();
			String outputPath = getPathInfo(OutputFile.parent);
			if (StringUtil.equals(key, "controller.java")) {
				outputPath = serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "controller" + StringPool.SLASH + entityName + "Controller" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "entity.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "entity" + StringPool.SLASH + entityName + "Entity" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "mapper.java")) {
				outputPath = serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "mapper" + StringPool.SLASH + entityName + "Mapper" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "mapper.xml")) {
				outputPath = serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "mapper" + StringPool.SLASH + "xml" + StringPool.SLASH + entityName + "Mapper" + StringPool.DOT_XML;
			}
			if (StringUtil.equals(key, "service.java")) {
				outputPath = serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "service" + StringPool.SLASH + "I" + entityName + "Service" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "serviceImpl.java")) {
				outputPath = serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "service" + StringPool.SLASH + "impl" + StringPool.SLASH + entityName + "ServiceImpl" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "entityVO.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "vo" + StringPool.SLASH + entityName + "VO" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "entityDTO.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "dto" + StringPool.SLASH + entityName + "DTO" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "entityExcel.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "excel" + StringPool.SLASH + entityName + "Excel" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "entityParam.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "param" + StringPool.SLASH + entityName + "Param" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "wrapper.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "wrapper" + StringPool.SLASH + entityName + "Wrapper" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "feign.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "feign" + StringPool.SLASH + "I" + entityName + "Client" + StringPool.DOT_JAVA;
			}

			if (StringUtil.equals(key, "feignclient.java")) {
				outputPath = serverOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "feign" + StringPool.SLASH + entityName + "Client" + StringPool.DOT_JAVA;
			}
			if (StringUtil.equals(key, "feignclientFallback.java")) {
				outputPath = apiOutputDir + StringPool.SLASH + packageName.replace(StringPool.DOT, StringPool.SLASH) + StringPool.SLASH + "feign" + StringPool.SLASH + entityName + "ClientFallback" + StringPool.DOT_JAVA;
			}
			outputFile(new File(String.valueOf(outputPath)), objectMap, value, Boolean.TRUE);
		});
	}

	/**
	 * 输出实体文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputEntity(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出Mapper文件(含xml)
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputMapper(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出service文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputService(TableInfo tableInfo, Map<String, Object> objectMap) {
	}

	/**
	 * 输出controller文件
	 *
	 * @param tableInfo 表信息
	 * @param objectMap 渲染数据
	 * @since 3.5.0
	 */
	@Override
	protected void outputController(TableInfo tableInfo, Map<String, Object> objectMap) {
	}
}
